package cn.itsource.campus.controller;

import org.apache.commons.io.FilenameUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@RestController
public class UploadController {

    //附件存储路径
    @Value("${file-store-path}")
    private String fileStorePath;

    @PostMapping("/upload")
    public void upload(String name, MultipartFile headImage ) throws IOException {
//        System.out.println(name);
//        System.out.println(headImage.getSize());//获取文件大小
//        System.out.println(headImage.getName()); //获取form表单中name对应的值
//        System.out.println(headImage.getContentType());//获取附件的mime类型
//        System.out.println(headImage.getOriginalFilename());//获取文件名

        //获取上传附件的字节输入流
        InputStream inputStream = headImage.getInputStream();

        //获取上传的附件名
        String originalFilename = headImage.getOriginalFilename();
        //动态获取文件的后缀
        String extension = FilenameUtils.getExtension(originalFilename);
        //获取附件的新名字
        String newFileName = System.currentTimeMillis() + "." + extension;

        //D:/upload/23423432422342.webp
        File file = new File(fileStorePath, newFileName);
        //如果父路径不存在，则创建
        if(!file.getParentFile().exists()){
            //创建父文件夹
            file.getParentFile().mkdirs();
        }
        //创建字节输出流
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        //文件上传的核心代码
        IOUtils.copy(inputStream, fileOutputStream);

        //关闭io刘
        fileOutputStream.close();
        inputStream.close();
    }


    /**
     * 下载附件
     * @param request  Http协议的请求对象
     * @param response http协议的响应对象
     */
    @GetMapping("/download")
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //设置附件的mime类型（只要下载附件，都是以io的方式进行向外输出）
        response.setContentType("application/octet-stream");
//        attachment;以弹框的方式进行下载
        response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode("1.webp", "UTF-8") );
        //设置一下附件名
        //找到源附件，转为字节输入流
        FileInputStream fis = new FileInputStream(new File(fileStorePath, "1702794412327.webp"));
        //以字节流的方式下载出去
        ServletOutputStream outputStream = response.getOutputStream();
        //下载核心代码
        IOUtils.copy(fis, outputStream);
        outputStream.close();
        fis.close();

    }

    public static void main(String[] args) {
        String str = "dskfjhsdkhdskfhksdhsdf.jpg";
        String extension = FilenameUtils.getExtension(str);
        System.out.println(extension);
    }
}
